import {substance} from './base'
import {Bullet} from './bullet'

class Player extends substance{
    height = '50px'
    width = '50px'
    speed=5
    // 移动状态0程序控制，1玩家控制
    move_status:number = 0
    img='/src/assets/play.png'
    // 弹幕实例
    barrage:any = new PlayBarrage0()
    barrage_wait:number = 0
    constructor(){
        super()
        this.update_position()
    }
    // 重置创建位置
    create_position(createpos:any){
        this.get_app_size()
        this.left = this.app_width/2
        this.top = this.app_height-50
    }
    numb:number = 0
    // 更新当前位置
    update_position(x:any,y:any){
        if (x ==undefined){
            super.update_position()
            return
        }
        let pos_x = this.left
        let pos_y = this.top
        if (typeof(x) == 'string'){
            pos_x = x !='' ? this.left + parseFloat(x+this.speed) : this.left
            pos_y = y !='' ? this.top + parseFloat(y+this.speed)  : this.top
        }
        
        this.get_app_size()
        this.left = pos_x>this.app_width+10 || pos_x<-10 ? this.left : pos_x
        this.top = pos_y>this.app_height+10 || pos_y<-10 ? this.top : pos_y
        
        
        if (this.barrage_wait>=this.barrage.wait_time){
            // 发射弹幕
            this.barrage_wait = 0
            this.numb++
            // if (this.numb>2){
            //     return
            // }
            return this.barrage.suit()
        }else{
            this.barrage_wait ++
        }
        return []
    }
    // 拾取道具
    get_tool(){
        
    }
    
}

// 玩家弹幕0
class PlayBarrage0{
    // 弹幕发射间隔
    wait_time:number=30
    // 子弹列表
    bullet_list = []
    // 弹幕等级
    level =2
    max_level=6
    // 弹幕升级
    level_up(){
        if (this.level>=6){
            return
        }
        this.wait_time -= 1
        this.level++
    }
    // 射击
    suit(){
        let [cenxf,cenyf] = this.get_center()
        let list = []
        let bullet = new Bullet({lineAngle:0,harm:this.level})
        bullet.create_position([cenxf,cenyf])
        list.push(bullet)
        if (this.level >= 2){
            let bullet2 = new Bullet({lineAngle:40})
            bullet2.create_position([cenxf+15,cenyf+5])
            let bullet3 = new Bullet({lineAngle:-40})
            bullet3.create_position([cenxf-15,cenyf+5])
            list.push(bullet2)
            list.push(bullet3)
        }
        if (this.level >= 3){
            let bullet4 = new Bullet({lineAngle:15})
            bullet4.create_position([cenxf,cenyf])
            let bullet5 = new Bullet({lineAngle:-15})
            bullet5.create_position([cenxf,cenyf])
            list.push(bullet4)
            list.push(bullet5)
        }    
        if (this.level >= 4){
            let bullet6 = new Bullet({harm:5,color:'#ff5500'})
            bullet6.create_position([cenxf-5,cenyf])
            let bullet7 = new Bullet({harm:5,color:'#ff5500'})
            bullet7.create_position([cenxf+5,cenyf])
            list.push(bullet6)
            list.push(bullet7)
        } 
        // for(let key=0 ; key<this.count;key++){
        //     list[key+this.count] = new Bullet({lineAngle:30})
        //     list[key+this.count].create_position([cenxf,cenyf,appx,appy])
        // }
        return list
    }
    get_center(){
        var dom = document.getElementById('player')
        let left = dom?.offsetLeft
        let top = dom?.offsetTop
        return [left,top]
    }
}


export {Player}